package com.hsj.demo.service;

import com.hsj.demo.dao.NoticeDao;
import com.hsj.demo.dao.UserDao;
import com.hsj.demo.dao.UserNoticeDao;
import com.hsj.demo.entity.Notice;
import com.hsj.demo.entity.User;
import com.hsj.demo.entity.UserNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NoticeService extends CurdService<NoticeDao, Notice> {
    @Autowired
    private NoticeDao noticeDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserNoticeDao userNoticeDao;

    public List<Notice> findByUser(User user){
        return noticeDao.findByUser(user);
    }

    public List<Notice> getAllNotice(){
        List<Notice> noticeList = noticeDao.findAll();
        for(Notice notice: noticeList){
            notice.setTargetUser(userDao.findById(notice.getTargetUserId()));
        }
        return noticeList;
    }

    public List<Notice> findNoticeByConditions(Map map){
        if(map.get("targetUsername")!=null&&map.get("targetUsername")!=""){
            map.put("targetUserId",userDao.findByUsername(map.get("targetUsername").toString()).getId());
        }
        List<Notice> noticeList = noticeDao.findNoticeByConditions(map);
        for(Notice notice:noticeList){
            notice.setTargetUser(userDao.findById(notice.getTargetUserId()));
        }
        return noticeList;
    }

    public Notice insertNotice(Map map, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user.getRole()!=2){
            return null;
        }
        User targetUser = userDao.findByUsername(map.get("targetUsername").toString());
        if(targetUser==null&&map.get("targetType").toString().equals("0")){
            return null;
        }
        Notice notice = new Notice();
        notice.setSourceUserId(user.getId());
        notice.setSourceUser(user);
        notice.setMessage(map.get("message").toString());
        notice.setTargetType(Integer.valueOf(map.get("targetType").toString()));
        notice.setSendTime(new Date());
        notice.setTargetUser(targetUser);
        if(notice.getTargetType()==0){
            notice.setTargetUserId(targetUser.getId());
        }
        noticeDao.insert(notice);

        if(notice.getTargetType()==0){
            UserNotice userNotice = new UserNotice();
            userNotice.setUserId(targetUser.getId());
            userNotice.setNoticeId(notice.getId());
            userNoticeDao.insert(userNotice);
        }else{
            List<User> userList = userDao.findAll();
            for(User i:userList){
                UserNotice userNotice = new UserNotice();
                userNotice.setUserId(i.getId());
                userNotice.setNoticeId(notice.getId());
                userNoticeDao.insert(userNotice);
            }
        }

        return notice;
    }

    public Integer deleteById(Integer userId,Integer noticeId){
        UserNotice userNotice = new UserNotice();
        userNotice.setNoticeId(noticeId);
        userNotice.setUserId(userId);
        userNoticeDao.deleteById(userNotice);
        return noticeDao.deleteById(noticeId);
    }
}
